const crypto = require('crypto')
const Admin = require('../../mongooses/rbac/Admin')
const UserSystemRoles = require('../../mongooses/rbac/UserSystemRoles')

class AuthController {
    async signin () {
        let username = ctx.request.body.username
        let password = ctx.request.body.password

        let admin = await admin.findOne({username: username}).lean();

        if (admin) {
            if (admin.password !== crypto.createHash('md5').update(password).digest('hex')){
                ctx.body = {
                    ret: -1,
                    msg: '密码错误'
                }
            }

            let user_id = admin._id
            let roles = await UserSystemRoles.findOne({user_id: user_id}).lean()

            let data = {
                user: admin,
                roles: roles
            }

            ctx.body = data
            
        } else {
            ctx.body = {
                ret: -1,
                msg: '用户不存在'
            }
        }
    }
    async signout () {
        
    }
}

module.exports = new AuthController()